package org.jeecg.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.service.IZkProduceReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 中科生产报表
 * @Author: jeecg-boot
 * @Date:   2024-07-26
 * @Version: V1.0
 */
@Api(tags="中科生产报表")
@RestController
@RequestMapping("/zkProduceReport/zkProduceReport")
@Slf4j
public class ZkProduceReportController extends JeecgController<ZkProduceReport, IZkProduceReportService> {
	@Autowired
	private IZkProduceReportService zkProduceReportService;

	 /**
	  * 日报表查询0
	  */
	 @IgnoreAuth
	 @AutoLog(value = "日报表查询0")
	 @ApiOperation(value="日报表查询0", notes="日报表查询0")
	 @GetMapping(value = "/queryZkProduceReportList")
	 public Result<?> queryZkProduceReportList(String productFloor) {
	 	List<List<ZkProduceReport>> list=new ArrayList();
	 	String workshopName=null;
	 	//整理参数
	 	if(productFloor.equals("2-1")||productFloor.equals("2-2")||productFloor.equals("2-3")||productFloor.equals("2-4")){
		 workshopName="中科成型2#炉";
		}
	 	if(productFloor.equals("4-1")||productFloor.equals("4-2")|productFloor.equals("4-3")||productFloor.equals("4-4")||productFloor.equals("4-5")){
			workshopName="中科成型4#炉";
		}
	 	if(productFloor.equals("5-1")||productFloor.equals("5-2")||productFloor.equals("5-3")){
		 	workshopName="中科成型5#炉";
		}
	 	 //整理时间
		 LocalDateTime now = LocalDateTime.now();
		 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
		 Integer hh=Integer.parseInt(formatter.format(now));

		 if(hh>=0&&hh<8){
		     //当前时间为夜班（查询前一天三个班数据——夜班数据）
			 LocalDateTime dayAgo = now.minusDays(1);
			 DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			 DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			 String mrpDsmBeginDate=formatterDay0.format(dayAgo);
			 String mrpDsmEndDate=formatterDay1.format(dayAgo);
			 List<ZkProduceReport> list1=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate,mrpDsmEndDate);
			 list.add(list1);
			 //当前时间为夜班（查询前一天三个班数据——白班数据）
			 DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			 String mrpDsmEndDate1=formatterDay2.format(dayAgo);
			 List<ZkProduceReport> list2=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmEndDate,mrpDsmEndDate1);
			 list.add(list2);
			 //当前时间为夜班（查询前一天三个班数据——中班班数据）(改)
			 String mrpDsmBeginDate1=formatterDay0.format(now);
			 List<ZkProduceReport> list3=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmEndDate1,mrpDsmBeginDate1);
			 list.add(list3);
		 }else if(hh>=8&&hh<16){
			//当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——夜班数据）
			 DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			 DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			 String mrpDsmBeginDate=formatterDay0.format(now);
			 String mrpDsmEndDate=formatterDay1.format(now);
			 List<ZkProduceReport> list1=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate,mrpDsmEndDate);
			 list.add(list1);
			 //当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——中班数据）（改）
			 LocalDateTime dayAgo = now.minusDays(1);
			 DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			 DateTimeFormatter formatterDay3 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			 String mrpDsmBeginDate1=formatterDay2.format(dayAgo);
			 String mrpDsmEndDate2=formatterDay3.format(now);
			 List<ZkProduceReport> list2=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate1,mrpDsmEndDate2);
			 list.add(list2);
			 //当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——白班数据）
			 DateTimeFormatter formatterDay4 = DateTimeFormatter.ofPattern("yyyy-MM-dd 8:00:00.000");
			 String mrpDsmBeginDate2=formatterDay4.format(dayAgo);
			 List<ZkProduceReport> list3=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate2,mrpDsmBeginDate1);
			 list.add(list3);
		 }else if(hh>=16&&hh<=23){
			//当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——白班数据）
			 DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			 DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			 String mrpDsmBeginDate1=formatterDay0.format(now);
			 String mrpDsmEndDate2=formatterDay1.format(now);
			 List<ZkProduceReport> list1=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate1,mrpDsmEndDate2);
			 list.add(list1);
			 //当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——夜班数据）
			 DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			 String mrpDsmBeginDate2=formatterDay2.format(now);
			 List<ZkProduceReport> list2=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate2,mrpDsmBeginDate1);
			 list.add(list2);
			 //当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——中班数据）（改）
			 LocalDateTime dayAgo = now.minusDays(1);
			 String mrpDsmBeginDate3=formatterDay1.format(dayAgo);
			 String mrpDsmEndDate3=formatterDay2.format(now);
			 List<ZkProduceReport> list3=this.zkProduceReportService.queryZkProduceReportList(workshopName,mrpDsmBeginDate3,mrpDsmEndDate3);
			 list.add(list3);
		 }

	 	return Result.OK(list);
	 }

	/**
	 * 平均合格率查询1
	 */
	@IgnoreAuth
	@AutoLog(value = "平均合格率查询1")
	@ApiOperation(value="平均合格率查询1", notes="平均合格率查询1")
	@GetMapping(value = "/queryZkProduceReportPass")
	public Result<?> queryZkProduceReportPass(String productFloor) {
		String workshopName=null;
		//整理参数
		if(productFloor.equals("2-1")||productFloor.equals("2-2")||productFloor.equals("2-3")||productFloor.equals("2-4")){
			workshopName="中科成型2#炉";
		}
		if(productFloor.equals("4-1")||productFloor.equals("4-2")|productFloor.equals("4-3")||productFloor.equals("4-4")||productFloor.equals("4-5")){
			workshopName="中科成型4#炉";
		}
		if(productFloor.equals("5-1")||productFloor.equals("5-2")||productFloor.equals("5-3")){
			workshopName="中科成型5#炉";
		}
		//获取当前时间
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String mrpDsmBeginDate=formatterDay.format(now);
		List<ZkProduceReportPass> list=this.zkProduceReportService.queryZkProduceReportPass(workshopName,mrpDsmBeginDate);
		for (ZkProduceReportPass item:list){
			item.setName(item.getGlcName()+"当日累计平均合格率");
		}

		return Result.OK(list);
	}

	/**
	 * 月度显示，每个供料机近30天合格率柱状图
	 */
	@IgnoreAuth
	@AutoLog(value = "月度显示，每个供料机近30天合格率柱状图")
	@ApiOperation(value="月度显示，每个供料机近30天合格率柱状图", notes="月度显示，每个供料机近30天合格率柱状图")
	@GetMapping(value = "/queryList")
	public Result<?> queryList(String productFloor) {
		String workshopName=null;
		List<List<ZkProduceReportWorkshopName>> list=new ArrayList<>();
		//整理参数
		if(productFloor.equals("2-1")||productFloor.equals("2-2")||productFloor.equals("2-3")||productFloor.equals("2-4")){
			workshopName="中科成型2#炉";
		}
		if(productFloor.equals("4-1")||productFloor.equals("4-2")|productFloor.equals("4-3")||productFloor.equals("4-4")||productFloor.equals("4-5")){
			workshopName="中科成型4#炉";
		}
		if(productFloor.equals("5-1")||productFloor.equals("5-2")||productFloor.equals("5-3")){
			workshopName="中科成型5#炉";
		}
		//获取当前时间
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		for(int i=1;i<=30;i++){
			//获取当前时间的前一天
			LocalDateTime time = now.minusDays(i);
			String time1=formatterDay.format(time);
			List<ZkProduceReportWorkshopName> list1=this.zkProduceReportService.queryList(time1,workshopName);
			for(ZkProduceReportWorkshopName item:list1){
				item.setTime(time1);
			}
			list.add(list1);
		}

		return Result.OK(list);
	}

	/**
	 * 月度显示，整台窑炉近30天的合格率曲线图
	 */
	@IgnoreAuth
	@AutoLog(value = "月度显示，整台窑炉近30天的合格率曲线图")
	@ApiOperation(value="月度显示，整台窑炉近30天的合格率曲线图", notes="月度显示，整台窑炉近30天的合格率曲线图")
	@GetMapping(value = "/queryProductFloor")
	public Result<?> queryProductFloor(String productFloor) {
		String workshopName=null;
		List<List<ZkProduceReportProductFloor>> list=new ArrayList<>();
		//整理参数
		if(productFloor.equals("2-1")||productFloor.equals("2-2")||productFloor.equals("2-3")||productFloor.equals("2-4")){
			workshopName="中科成型2#炉";
		}
		if(productFloor.equals("4-1")||productFloor.equals("4-2")|productFloor.equals("4-3")||productFloor.equals("4-4")||productFloor.equals("4-5")){
			workshopName="中科成型4#炉";
		}
		if(productFloor.equals("5-1")||productFloor.equals("5-2")||productFloor.equals("5-3")){
			workshopName="中科成型5#炉";
		}
		//获取当前时间
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatterDay = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		for(int i=1;i<=30;i++){
			//获取当前时间的前一天
			LocalDateTime time = now.minusDays(i);
			String time1=formatterDay.format(time);
			List<ZkProduceReportProductFloor> list2=this.zkProduceReportService.queryProductFloor(time1,workshopName);
			for(ZkProduceReportProductFloor item:list2){
				item.setTime(time1);
			}
			list.add(list2);
		}
		return Result.OK(list);
	}

	/**
	 * 近三个班供料机合格率
	 */
	@IgnoreAuth
	@AutoLog(value = "近三个班供料机合格率")
	@ApiOperation(value="近三个班供料机合格率", notes="近三个班供料机合格率")
	@GetMapping(value = "/queryGlcname")
	public Result<?> queryGlcname(String productFloor) {
		List<List<ZkProduceReportGlcname>> list=new ArrayList();
		String workshopName=null;
		//整理参数
		if(productFloor.equals("2-1")||productFloor.equals("2-2")||productFloor.equals("2-3")||productFloor.equals("2-4")){
			workshopName="中科成型2#炉";
		}
		if(productFloor.equals("4-1")||productFloor.equals("4-2")|productFloor.equals("4-3")||productFloor.equals("4-4")||productFloor.equals("4-5")){
			workshopName="中科成型4#炉";
		}
		if(productFloor.equals("5-1")||productFloor.equals("5-2")||productFloor.equals("5-3")){
			workshopName="中科成型5#炉";
		}
		//整理时间
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
		Integer hh=Integer.parseInt(formatter.format(now));

		if(hh>=0&&hh<8){
			//当前时间为夜班（查询前一天三个班数据——夜班数据）
			LocalDateTime dayAgo = now.minusDays(1);
			DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			String mrpDsmBeginDate=formatterDay0.format(dayAgo);
			String mrpDsmEndDate=formatterDay1.format(dayAgo);
			List<ZkProduceReportGlcname> list1=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate,mrpDsmEndDate);
			list.add(list1);
			//当前时间为夜班（查询前一天三个班数据——白班数据）
			DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			String mrpDsmEndDate1=formatterDay2.format(dayAgo);
			List<ZkProduceReportGlcname> list2=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmEndDate,mrpDsmEndDate1);
			list.add(list2);
			//当前时间为夜班（查询前一天三个班数据——中班班数据）(改)
			String mrpDsmBeginDate1=formatterDay0.format(now);
			List<ZkProduceReportGlcname> list3=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmEndDate1,mrpDsmBeginDate1);
			list.add(list3);
		}else if(hh>=8&&hh<16){
			//当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——夜班数据）
			DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			String mrpDsmBeginDate=formatterDay0.format(now);
			String mrpDsmEndDate=formatterDay1.format(now);
			List<ZkProduceReportGlcname> list1=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate,mrpDsmEndDate);
			list.add(list1);
			//当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——中班数据）（改）
			LocalDateTime dayAgo = now.minusDays(1);
			DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			DateTimeFormatter formatterDay3 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			String mrpDsmBeginDate1=formatterDay2.format(dayAgo);
			String mrpDsmEndDate2=formatterDay3.format(now);
			List<ZkProduceReportGlcname> list2=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate1,mrpDsmEndDate2);
			list.add(list2);
			//当前时间为白班（查询当天夜班数据，前一天中班数据，前一天白班数据——白班数据）
			DateTimeFormatter formatterDay4 = DateTimeFormatter.ofPattern("yyyy-MM-dd 8:00:00.000");
			String mrpDsmBeginDate2=formatterDay4.format(dayAgo);
			List<ZkProduceReportGlcname> list3=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate2,mrpDsmBeginDate1);
			list.add(list3);
		}else if(hh>=16&&hh<=23){
			//当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——白班数据）
			DateTimeFormatter formatterDay0 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00:00.000");
			DateTimeFormatter formatterDay1 = DateTimeFormatter.ofPattern("yyyy-MM-dd 16:00:00.000");
			String mrpDsmBeginDate1=formatterDay0.format(now);
			String mrpDsmEndDate2=formatterDay1.format(now);
			List<ZkProduceReportGlcname> list1=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate1,mrpDsmEndDate2);
			list.add(list1);
			//当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——夜班数据）
			DateTimeFormatter formatterDay2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00.000");
			String mrpDsmBeginDate2=formatterDay2.format(now);
			List<ZkProduceReportGlcname> list2=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate2,mrpDsmBeginDate1);
			list.add(list2);
			//当前时间为中班（查询当天白班数据，当天夜班数据，前一天中班数据——中班数据）（改）
			LocalDateTime dayAgo = now.minusDays(1);
			String mrpDsmBeginDate3=formatterDay1.format(dayAgo);
			String mrpDsmEndDate3=formatterDay2.format(now);
			List<ZkProduceReportGlcname> list3=this.zkProduceReportService.queryGlcname(workshopName,mrpDsmBeginDate3,mrpDsmEndDate3);
			list.add(list3);
		}

		return Result.OK(list);
	}

}
